package order

import (
	"log"
	"strconv"
	"time"
	"xiehui-service/app/common/request"
	"xiehui-service/app/common/response"
	"xiehui-service/app/services/erp_services"
	"xiehui-service/global"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// 创建任务单
func AddOrderTask(c *gin.Context) {
	var args request.ThirdOrder
	if err := c.ShouldBindJSON(&args); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(args, err))
		return
	}

	var vLineNumber = global.App.Config.App.CompanyId
	// var vMixerNo = global.App.Config.App.MachineNo
	// var vMixerNoStr = strconv.FormatInt(vMixerNo, 10)

	// unioNo := fmt.Sprintf("RWD%02d%02d%s", vLineNumber, vMixerNo, sdOrder.ID)
	erpOrder, err := erp_services.OrderTaskService.GetOrderTaskByUnioNo(args.DDJL_YHBH)
	log.Println("查询erpOrder ID==>>", erpOrder.ID.ID, err)

	// 组织数据
	erpOrder.CustomerName = args.DDJL_DWMC    //客户名称
	erpOrder.ProjectName = args.DDJL_XMMC     //项目名称
	erpOrder.ProjectSubName = args.DDJL_XMMC  //工程名称
	erpOrder.ShippingAddress = args.DDJL_YHDD //收货地址
	erpOrder.BuildPosition = args.DDJL_WBSMC  //浇筑部位
	erpOrder.BuildMethod = ""                 //浇筑方式

	buildId, _ := strconv.ParseInt(args.DDJL_FBDWID, 10, 64)
	erpOrder.BuildId = buildId              //施工单位ID(分包站)
	erpOrder.BuildName = args.DDJL_FBDWMC   //施工单位名称(分包站)
	erpOrder.BindMixerNo = args.DDJL_SCCJID //拌台

	concreteCategoryId, _ := strconv.ParseInt(args.DDJL_WZID, 10, 64)
	erpOrder.ConcreteCategoryId = concreteCategoryId //砼品种ID
	erpOrder.ConcreteCategory = args.DDJL_WZMC       //砼品种名称

	erpOrder.Strength = ""           //强度等级
	erpOrder.Seepage = ""            //抗渗等级
	erpOrder.Slumps = args.DDJL_YHMS //坍落度
	erpOrder.CementSpecification = ""
	erpOrder.StoneSpecification = ""
	erpOrder.AdditiveSpecification = ""
	erpOrder.SpecialRequirements = ""
	erpOrder.WorkMixtureNo = ""
	erpOrder.MortarMixtureNo = ""
	erpOrder.MixingTime = 0

	erpOrder.PlanQuantity = args.DDJL_SL //计划方量
	planTime, _ := time.Parse(time.DateTime, args.DDJL_SYSJ)
	erpOrder.FirstPlanTime = &planTime //计划时间

	erpOrder.CountVehicle = 0
	erpOrder.ConcreteQuantity = 0

	//记录已存在
	if erpOrder.ID.ID > 0 {
		err := erp_services.OrderTaskService.Update(erpOrder, true)
		if err != nil {
			global.App.Log.Info("更新ERP任务单失败：", zap.Any("失败原因", err))
			log.Println("更新任务单失败：", err)
		}
		log.Println("更新任务单成功", erpOrder.ID.ID)
		global.App.Log.Info("更新ERP任务单成功：", zap.Any("ERP任务单ID", erpOrder.ID.ID))
	}

	// 新建任务单
	erpOrder.LineNumber = vLineNumber
	erpOrder.UnionNo = ""
	erpOrder.TaskNo = args.DDJL_YHBH
	erpOrder.Remark = args.DDJL_YHMS

	// strTime := utils.TimeFormat(sdOrder.BegTim)
	// if strTime != "" {
	// 	plantime, err := time.ParseInLocation(time.DateTime, strTime, time.Local)
	// 	if err == nil {
	// 		erpOrder.PlanTime = &plantime
	// 	}
	// }

	// 固定字段
	erpOrder.VerifyStatus = 3
	erpOrder.TaskStatus = 3 //3生产中 10确认完成任务单

	orderId, err := erp_services.OrderTaskService.Create(erpOrder)
	log.Println("新增ERP任务单==>>", orderId, err)
	if err != nil {
		global.App.Log.Info("新增ERP任务单失败:", zap.Any("失败原因", err))
	}
	global.App.Log.Info("新增ERP任务单成功:", zap.Any("ERP任务单ID", orderId))

	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, map[string]int64{"order_id": int64(orderId)})
}

func OrderTask(c *gin.Context) {
	var form request.List
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	orderTask, err := erp_services.OrderTaskService.GetOrderTaskInfo(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, orderTask)
}

func AutoHandle(c *gin.Context) {
	var form request.HandleArgs
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	message, err := erp_services.AutoDispatchService.AutoHandle(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, message)
}
